package cn.schoolwow.quickdao.module.database.dml.option.kit;

import cn.schoolwow.quickdao.domain.database.dml.CheckStrategy;
import cn.schoolwow.quickdao.domain.database.dml.DatabaseManipulation;
import cn.schoolwow.quickdao.domain.database.dml.UpdateType;
import cn.schoolwow.quickdao.module.database.dml.common.domain.ManipulationOption;
import cn.schoolwow.quickdao.module.database.parent.kit.DatabaseDAOImpl;
import cn.schoolwow.quickflow.QuickFlow;

import java.util.Arrays;

public class DatabaseManipulationOptionImpl extends DatabaseDAOImpl implements DatabaseManipulationOption {
    private ManipulationOption manipulationOption;

    public DatabaseManipulationOptionImpl(QuickFlow quickFlow) {
        super(quickFlow);
        this.manipulationOption = quickFlow.getContextThreadLocalInstanceData(ManipulationOption.class);
    }

    @Override
    public DatabaseManipulation returnGeneratedKeys(boolean returnGeneratedKeys) {
        manipulationOption.returnGeneratedKeys = returnGeneratedKeys;
        if(manipulationOption.returnGeneratedKeys){
            //开启了获取自增id就无法实现批处理
            manipulationOption.batch = false;
        }else{
            manipulationOption.batch = true;
        }
        return manipulationOption.databaseManipulation;
    }

    @Override
    public DatabaseManipulation batch(boolean batch) {
        manipulationOption.batch = batch;
        if (manipulationOption.batch) {
            //开启了批处理就无法获取自增id
            manipulationOption.returnGeneratedKeys = false;
        }else{
            manipulationOption.returnGeneratedKeys = true;
        }
        return manipulationOption.databaseManipulation;
    }

    @Override
    public DatabaseManipulation perBatchCount(int perBatchCount) {
        manipulationOption.perBatchCount = perBatchCount;
        return manipulationOption.databaseManipulation;
    }

    @Override
    public DatabaseManipulation partColumn(String... fieldNames) {
        manipulationOption.partColumnSet.addAll(Arrays.asList(fieldNames));
        return manipulationOption.databaseManipulation;
    }

    @Override
    public DatabaseManipulation excludeColumn(String... fieldNames) {
        manipulationOption.excludeColumnSet.addAll(Arrays.asList(fieldNames));
        return manipulationOption.databaseManipulation;
    }

    @Override
    public DatabaseManipulation uniqueFieldNames(String... uniqueFieldNames) {
        manipulationOption.uniqueFieldNames.addAll(Arrays.asList(uniqueFieldNames));
        return manipulationOption.databaseManipulation;
    }

    @Override
    public DatabaseManipulation updateType(UpdateType updateType) {
        manipulationOption.updateType = updateType;
        return manipulationOption.databaseManipulation;
    }

    @Override
    public DatabaseManipulation checkStrategy(CheckStrategy checkStrategy) {
        manipulationOption.checkStrategy = checkStrategy;
        return manipulationOption.databaseManipulation;
    }

}
